Page({
    data: {
        awardList: []
        , indexSelect: 0
        , isRunning: false
        , prize: false
        , close: false
        , lottert: 0
        , time: false
        , title: ''
        , scene: ''
        , oppty: 0
        , donghua:false
        , kaiqian:false
        , jiang:false
        , qianarr:[
            '',
            'https://wxapp.yzhihe.com/attachment/images/chouqian/jlq.gif',
            'https://wxapp.yzhihe.com/attachment/images/chouqian/sssq.gif',
            'https://wxapp.yzhihe.com/attachment/images/chouqian/ssq.gif',
            'https://wxapp.yzhihe.com/attachment/images/chouqian/sq.gif',
            'https://wxapp.yzhihe.com/attachment/images/chouqian/zsq.gif',
            'https://wxapp.yzhihe.com/attachment/images/chouqian/ryq.gif',
            'https://wxapp.yzhihe.com/attachment/images/chouqian/hyq.gif',
            'https://wxapp.yzhihe.com/attachment/images/chouqian/fqq.gif'
        ]
        , qianname:[
            '',
            '锦鲤签',
            '上上上签',
            '上上签',
            '上签',
            '中上签',
            '如意签',
            '好运签',
            '福气签'
        ]
    }
    , onLoad: function (options) {
        getApp().page.onLoad(this, options);
        var zz_that = this;

        if (options.hasOwnProperty('scene')) {
            zz_that.data.scene = options.scene;
        }

        getApp().request({
            url: getApp().api.draw.setting
            , success: function (result) {
                if (result.code == 0) {
                    var title = result.data.title;
                    if (title) {
                        getApp().core.setNavigationBarTitle({
                            title: title
                        });
                        zz_that.setData({
                            title: title
                        });
                    }
                }
            },
        });
    }
    , onShow: function () {
        getApp().page.onShow(this);
        var zz_that = this;

        getApp().core.showLoading({
            title: '加载中',
        });

        getApp().request({
            url: getApp().api.draw.index
            , data: {scene: zz_that.data.scene}
            , complete: function () {
                getApp().core.hideLoading();
            }
            , success: function (result) {
                zz_that.setData(result.data);
                zz_that.setData({
                    awardList: result.data.list
                });
            }
        });
    }
    , startGame: function () {
        var zz_that = this;
        var audio = wx.createInnerAudioContext();
        if (zz_that.data.isRunning) {
            return false;
        }

        var title = '';
        if (zz_that.data.oppty == 0) {
            title = '您抽签机会不足';
        }

        if (!zz_that.data.integral) {
            title = '您积分不足';
        }

        if (!zz_that.data.time) {
            title = '活动未开始或已结束';
        }

        if (title) {
            getApp().core.showModal({
                title: '很抱歉'
                , content: title
                , showCancel: false
                , success: function (result) {
                    if (result.confirm) {
                        zz_that.setData({
                            isRunning: false
                        });
                    }
                }
            })
            return false;
        }

        zz_that.setData({
            isRunning: true
            , lottert: 0
            , indexSelect: 0
        });

        var indexSelect = zz_that.data.indexSelect;
        var i = 0;
        var list = zz_that.data.awardList;

        wx.showLoading({
            title: '正在抽签中...'
        });

        var timer = setInterval(function () {
            
            indexSelect++;
            indexSelect = indexSelect % 8;
            i += 30;
            zz_that.setData({
                indexSelect: indexSelect
            });
            if (zz_that.data.lottert > 0 && indexSelect + 1 == zz_that.data.lottert) {
                clearInterval(timer);
                wx.hideLoading();

                if (list[indexSelect].type == 5) {
                    var prize = 1;
                } else {
                    var prize = 2;
                }
                audio.src = "https://wxapp.yzhihe.com/attachment/images/chouqian/yao.mp3";
                audio.play()
        
                zz_that.setData({
                    isRunning: false
                    , name: list[indexSelect].name
                    , num: list[indexSelect].id
                    , prize: prize
                    , donghua: true
                });
                setTimeout(() => {
                    zz_that.setData({

                    })
                },3000)
                setTimeout(() => {
                    zz_that.setData({
                        kaiqian:true
                    })

                    audio.src = "https://wxapp.yzhihe.com/attachment/images/chouqian/zhongjiang.wav";
                    audio.play()
                }, 3500);
            }
        }, (200 + i));

        getApp().request({
            url: getApp().api.draw.lottery
            , data: {scene: zz_that.data.scene}
            , success: function (result) {
                if (result.code == 1) {
                    clearInterval(timer);
                    wx.hideLoading();

                    getApp().core.showModal({
                        title: '很抱歉'
                        , content: result.msg ? result.msg : '网络错误'
                        , showCancel: false
                        , success: function (result) {
                            if (result.confirm) {
                                zz_that.setData({
                                    isRunning: false
                                });
                            }
                        }
                    });
                    return false;
                }

                if (result.msg == '积分不足') {
                    zz_that.setData({
                        integral: false
                    });
                }

                var id = result.data.id;
                list.forEach(function (item, index) {
                    if (item.id == id) {
                        setTimeout(function () {
                            zz_that.setData({
                                lottert: index + 1
                                , oppty: result.data.oppty
                            });
                        }, 2000);
                    }
                });
            }
        });
    }
    , kaiqian:function(){
        this.setData({
            prize:2,
            kaiqian:false,
            jiang:true
        })
    }
    , pondClose: function () {
        this.setData({
            prize: false,
            donghua:false
        })
    }
    , onHide: function () {
        getApp().page.onHide(this);
    }







    , onShareAppMessage: function () {
        getApp().page.onShareAppMessage(this);
        this.setData({
            share_modal_active: false
        });
        var user_info = getApp().getUser();
        var res = {
            path: "/draw/draw/draw?user_id=" + user_info.id,
            title: this.data.title ? this.data.title : '九宫格抽签',
        };
        setTimeout(function () {
            return res;
        }, 500);
    },

    showShareModal: function () {
        this.setData({
            share_modal_active: "active",
        });
    },

    shareModalClose: function () {
        this.setData({
            share_modal_active: '',
        })
    },

    getGoodsQrcode: function () {
        var zz_that = this;
        zz_that.setData({
            qrcode_active: "active",
            share_modal_active: "",
        });
        if (zz_that.data.goods_qrcode) return true

        getApp().request({
            url: getApp().api.pond.qrcode,
            success: function (res) {
                if (res.code == 0) {
                    zz_that.setData({
                        goods_qrcode: res.data.pic_url,
                    });
                }
                if (res.code == 1) {
                    zz_that.goodsQrcodeClose();
                    getApp().core.showModal({
                        title: "提示",
                        content: res.msg,
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {
                            }
                        }
                    });
                }
            },
        });
    },

    qrcodeClick: function (e) {
        var src = e.currentTarget.dataset.src;
        getApp().core.previewImage({
            urls: [src],
        });
    },
    qrcodeClose: function () {
        var zz_that = this;
        zz_that.setData({
            qrcode_active: "",
        });
    },

    saveQrcode: function () {
        var zz_that = this;
        if (!getApp().core.saveImageToPhotosAlbum) {
            getApp().core.showModal({
                title: '提示',
                content: '当前版本过低，无法使用该功能，请升级到最新版本后重试。',
                showCancel: false,
            });
            return;
        }

        getApp().core.showLoading({
            title: "正在保存图片",
            mask: false,
        });

        getApp().core.downloadFile({
            url: zz_that.data.goods_qrcode,
            success: function (e) {
                getApp().core.showLoading({
                    title: "正在保存图片",
                    mask: false,
                });
                getApp().core.saveImageToPhotosAlbum({
                    filePath: e.tempFilePath,
                    success: function () {
                        getApp().core.showModal({
                            title: '提示',
                            content: '商品海报保存成功',
                            showCancel: false,
                        });
                    },
                    fail: function (e) {
                        getApp().core.showModal({
                            title: '图片保存失败',
                            content: e.errMsg,
                            showCancel: false,
                        });
                    },
                    complete: function (e) {
                        getApp().core.hideLoading();
                    }
                });
            },
            fail: function (e) {
                getApp().core.showModal({
                    title: '图片下载失败',
                    content: e.errMsg + ";" + zz_that.data.goods_qrcode,
                    showCancel: false,
                });
            },
            complete: function (e) {
                getApp().core.hideLoading();
            }
        });
    },

    goodsQrcodeClose: function () {
        var zz_that = this;
        zz_that.setData({
            goods_qrcode_active: "",
            no_scroll: false,
        });
    },
});
